package com.dm.library.widgets;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.AbsListView;

/**
 * 版 权: 深圳市迪蒙网络科技有限公司
 * 描 述: 解决包含ListView，滑动冲突问题
 * <p>因为SwipeRefreshLayout只能包含一个子控件，如果该子控件是ListView，往上滑动出出现refresh效果。如果ListView没有到达顶部，SwipeRefreshLayout的enable应该是false才对<p/>
 * 修 改 人: sushuo
 * 修改时间: 2017-03-15
 */
public class DMContainListViewSwipeRefreshLayout extends DMSwipeRefreshLayout {

    private View view;

    public DMContainListViewSwipeRefreshLayout(Context context) {
        super(context);
    }

    public DMContainListViewSwipeRefreshLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    /**
     * 外部传入ListView控件
     *
     * @param view
     */
    public void setViewGroup(View view) {
        this.view = view;
    }

    @Override
    public boolean canChildScrollUp() {
        if (view != null && view instanceof AbsListView) {
            final AbsListView absListView = (AbsListView) view;
            return absListView.getChildCount() > 0
                    && (absListView.getFirstVisiblePosition() > 0 || absListView.getChildAt(0).getTop() < absListView.getPaddingTop());
        }
        return super.canChildScrollUp();
    }

}
